﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DaUngDung
{
    public partial class Form2 : Form
    {
        public Form1 f1;
        public Form2()
        {
            InitializeComponent();
            delete();
        }
        double so1, so2, kq;
        string pheptoan;        

        void delete()
        {
            txt1.Text = "";
            txt2.Text = "";
            txtKQ.Text = "";
            so1 = 0;
            so2 = 0;
            kq = 0;
        }

        //+ - * /
        private void bttDau_Click(object sender, EventArgs e)
        {
            try
            {
                Button mydau = (Button)sender;
                pheptoan = mydau.Text;
                so1 = double.Parse(txt1.Text);
                so2 = double.Parse(txt2.Text);
                switch (pheptoan)
                {
                    case "+": kq = so1 + so2;
                        break;
                    case "-": kq = so1 - so2;
                        break;
                    case "*": kq = so1 * so2;
                        break;
                    case "/":
                        if (so2 == 0)
                        {
                            txtKQ.Text = "";
                            kq = 0;
                            MessageBox.Show("Loi khong chia duoc cho 0 ");                        
                        }
                        else
                        kq = so1 / so2;
                        break;                   
                }
            }          
            catch(Exception)
            {
                txtKQ.Text = "";
                kq = 0;
                MessageBox.Show("nhap loi!");

            }
            txtKQ.Text = kq.ToString();
        }

        //btn_delete
        private void bttDelete_Click(object sender, EventArgs e)
        {
            delete();
        }

        private void txt_TextChanged(object sender, EventArgs e)
        {
            TextBox A = (TextBox)sender;
            double a;
            try
            {
                a = double.Parse(A.Text);
            }
            catch (Exception)
            {
                A.Text = "";
            }
        }

        private void btnTroVe_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void Form2_FormClosed(object sender, FormClosedEventArgs e)
        {
            f1.Show();
        }
    }
}
